<template>
  <page-header-wrapper :title="false">
    <a-card
      :body-style="{ padding: '24px 32px' }"
      :bordered="false"
      :tabList="tabList"
      :activeTabKey="activeTabKey"
      @tabChange="
        (key) => {
          this.activeTabKey = key
          handleSearch(queryParam)
        }
      "
    >
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="日期">
                <a-range-picker v-model="queryParam.date" format="YYYY-MM-DD" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="品牌">
                <a-select v-decorator="queryParam.brandId" placeholder="请选择品牌">
                  <a-select-option :value="i.id" v-for="i in brandOption" :key="i.id">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="分类">
                <a-select v-decorator="queryParam.gdClsId" placeholder="请选择分类">
                  <a-select-option :value="i.id" v-for="i in option" :key="i.id">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.queryCondition" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="loading" @click="handleSearch(queryParam)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table
        v-if="activeTabKey === '1'"
        rowKey="key"
        :columns="storecolumns"
        :data-source="loadData"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template> <a-tag color="#428bca" @click="handleDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
      <a-table
        v-if="activeTabKey === '2'"
        rowKey="key"
        :columns="supplycolumns"
        :data-source="loadData"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template> <a-tag color="#428bca" @click="handleDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
      <a-table
        v-if="activeTabKey === '3'"
        rowKey="key"
        :columns="goodscolumns"
        :data-source="loadData"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template> <a-tag color="#428bca" @click="handleDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
      <a-table
        v-if="activeTabKey === '4'"
        rowKey="key"
        :columns="channelcolumns"
        :data-source="loadData"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template> <a-tag color="#428bca" @click="handleDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
      <a-table
        v-if="activeTabKey === '5'"
        rowKey="key"
        :columns="areacolumns"
        :data-source="loadData"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template> <a-tag color="#428bca" @click="handleDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
      <a-table
        v-if="activeTabKey === '6'"
        rowKey="key"
        :columns="empcolumns"
        :data-source="loadData"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template> <a-tag color="#428bca" @click="handleDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { omit } from 'lodash-es'
import { getGdBrandListUsingPOST } from '@/api/api/shangpinguanli.ts'
import { getGdClassListUsingPOST } from '@/api/api/shangpinguanli.ts'
import {
  getStoreWholeStatListUsingPOST,
  getSupplyWholeStatListUsingPOST,
  getGoodsWholeStatListUsingPOST,
  getChannelWholeStatListUsingPOST,
  getAreaWholeStatListUsingPOST,
  getEmpWholeStatListUsingPOST,
} from '@/api/api/tongjibaobiao.ts'
export default {
  name: 'WechatShop',
  data() {
    return {
      brandOption: [],
      option: [],
      mdl: null,
      visible: false,
      tabList: [
        { key: '1', tab: '店面统计' },
        { key: '2', tab: '经销商统计' },
        { key: '3', tab: '商品统计' },
        { key: '4', tab: '渠道统计' },
        { key: '5', tab: '区域统计' },
        { key: '6', tab: '员工统计' },
      ],
      activeTabKey: '1',
      confirmLoading: false,
      // 高级搜索 展开/关闭
      advanced: false,
      loading: false,
      pagination: {
        total: 0,
      },
      // 查询参数
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        date: [moment().startOf('months').format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')],
        brandId: '',
        gdClsId: '',
        queryCondition: '',
        status: '',
      },
      // 表头
      storecolumns: [
        {
          title: '店名',
          dataIndex: 'name',
        },
        {
          title: '出库数量',
          dataIndex: 'outNum',
        },
        {
          title: '出库金额',
          dataIndex: 'outAmount',
        },
        {
          title: '出库毛利',
          dataIndex: 'outGrossProfit',
        },
        {
          title: '退库数量',
          dataIndex: 'returnNum',
        },
        {
          title: '退库金额',
          dataIndex: 'returnAmount',
        },
        {
          title: '退库损失毛利',
          dataIndex: 'returnGrossProfit',
        },
        {
          title: '毛利',
          dataIndex: 'grossProfit',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      supplycolumns: [
        {
          title: '经销商',
          dataIndex: 'name',
        },
        {
          title: '出库数量',
          dataIndex: 'outNum',
        },
        {
          title: '出库金额',
          dataIndex: 'outAmount',
        },
        {
          title: '出库毛利',
          dataIndex: 'outGrossProfit',
        },
        {
          title: '退库数量',
          dataIndex: 'returnNum',
        },
        {
          title: '退库金额',
          dataIndex: 'returnAmount',
        },
        {
          title: '退库损失毛利',
          dataIndex: 'returnGrossProfit',
        },
        {
          title: '毛利',
          dataIndex: 'grossProfit',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      goodscolumns: [
        {
          title: '商品名',
          dataIndex: 'name',
        },
        {
          title: '出库数量',
          dataIndex: 'outNum',
        },
        {
          title: '出库金额',
          dataIndex: 'outAmount',
        },
        {
          title: '出库毛利',
          dataIndex: 'outGrossProfit',
        },
        {
          title: '退库数量',
          dataIndex: 'returnNum',
        },
        {
          title: '退库金额',
          dataIndex: 'returnAmount',
        },
        {
          title: '退库损失毛利',
          dataIndex: 'returnGrossProfit',
        },
        {
          title: '毛利',
          dataIndex: 'grossProfit',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      channelcolumns: [
        {
          title: '渠道',
          dataIndex: 'name',
        },
        {
          title: '出库数量',
          dataIndex: 'outNum',
        },
        {
          title: '出库金额',
          dataIndex: 'outAmount',
        },
        {
          title: '出库毛利',
          dataIndex: 'outGrossProfit',
        },
        {
          title: '退库数量',
          dataIndex: 'returnNum',
        },
        {
          title: '退库金额',
          dataIndex: 'returnAmount',
        },
        {
          title: '退库损失毛利',
          dataIndex: 'returnGrossProfit',
        },
        {
          title: '毛利',
          dataIndex: 'grossProfit',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      areacolumns: [
        {
          title: '区域',
          dataIndex: 'name',
        },
        {
          title: '出库数量',
          dataIndex: 'outNum',
        },
        {
          title: '出库金额',
          dataIndex: 'outAmount',
        },
        {
          title: '出库毛利',
          dataIndex: 'outGrossProfit',
        },
        {
          title: '退库数量',
          dataIndex: 'returnNum',
        },
        {
          title: '退库金额',
          dataIndex: 'returnAmount',
        },
        {
          title: '退库损失毛利',
          dataIndex: 'returnGrossProfit',
        },
        {
          title: '毛利',
          dataIndex: 'grossProfit',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      empcolumns: [
        {
          title: '员工',
          dataIndex: 'name',
        },
        {
          title: '出库数量',
          dataIndex: 'outNum',
        },
        {
          title: '出库金额',
          dataIndex: 'outAmount',
        },
        {
          title: '出库毛利',
          dataIndex: 'outGrossProfit',
        },
        {
          title: '退库数量',
          dataIndex: 'returnNum',
        },
        {
          title: '退库金额',
          dataIndex: 'returnAmount',
        },
        {
          title: '退库损失毛利',
          dataIndex: 'returnGrossProfit',
        },
        {
          title: '毛利',
          dataIndex: 'grossProfit',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],

      // 加载数据方法 必须为 Promise 对象
      loadData: [],
    }
  },

  mounted() {
    this.getOption()
    this.getClassOption()
    this.handleSearch(this.queryParam)
  },
  methods: {
    getClassOption() {
      getGdClassListUsingPOST(this.optionParam)
        .then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    getOption() {
      getGdBrandListUsingPOST(this.optionParam)
        .then((res) => {
          this.brandOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleSearch(val) {
      this.loading = true
      const params = {
        ...val,
        startTime: val?.date?.[0] ? moment(val?.date?.[0]).format('YYYY-MM-DD') : null,
        endTime: val?.date?.[1] ? moment(val?.date?.[1]).format('YYYY-MM-DD') : null,
      }
      switch (this.activeTabKey) {
        case ('1'):
          getStoreWholeStatListUsingPOST({ ...omit(params, ['date']) })
            .then((res) => {
              this.loadData = res.data.list
              this.loading = false
              this.queryParam.pageNo = this.pagination.current
              this.pagination = { ...this.pagination, total: Number(res.data.total) }
            })
            .catch((error) => {
              this.loading = false
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
          break
        case ('2'):
          getSupplyWholeStatListUsingPOST({ ...omit(params, ['date']) })
            .then((res) => {
              this.loadData = res.data.list
              this.loading = false
              this.queryParam.pageNo = this.pagination.current
              this.pagination = { ...this.pagination, total: Number(res.data.total) }
            })
            .catch((error) => {
              this.loading = false
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
          break
        case ('3'):
          getGoodsWholeStatListUsingPOST({ ...omit(params, ['date']) })
            .then((res) => {
              this.loadData = res.data.list
              this.loading = false
              this.queryParam.pageNo = this.pagination.current
              this.pagination = { ...this.pagination, total: Number(res.data.total) }
            })
            .catch((error) => {
              this.loading = false
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
          break
        case ('4'):
          getChannelWholeStatListUsingPOST({ ...omit(params, ['date']) })
            .then((res) => {
              this.loadData = res.data.list
              this.loading = false
              this.queryParam.pageNo = this.pagination.current
              this.pagination = { ...this.pagination, total: Number(res.data.total) }
            })
            .catch((error) => {
              this.loading = false
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
          break
        case ('5'):
          getAreaWholeStatListUsingPOST({ ...omit(params, ['date']) })
            .then((res) => {
              this.loadData = res.data.list
              this.loading = false
              this.queryParam.pageNo = this.pagination.current
              this.pagination = { ...this.pagination, total: Number(res.data.total) }
            })
            .catch((error) => {
              this.loading = false
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
          break
        case ('6'):
          getEmpWholeStatListUsingPOST({ ...omit(params, ['date']) })
            .then((res) => {
              this.loadData = res.data.list
              this.loading = false
              this.queryParam.pageNo = this.pagination.current
              this.pagination = { ...this.pagination, total: Number(res.data.total) }
            })
            .catch((error) => {
              this.loading = false
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
          break
      }
    },
    handleTableChange(pagination) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
      pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.handleSearch({
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: this.queryParam.queryCondition,
        date: this.queryParam.date,
        brandId: this.queryParam.brandId,
        gdClsId: this.queryParam.gdClsId,
      })
    },
    handleDetail(record) {
      switch (this.activeTabKey) {
        case '1':
          this.$router.push({
         path: `/report/WholesaleDetail/${record?.id}` ,
         query: {
          startTime: moment(this.queryParam.date[0]).format('YYYY-MM-DD'),
          endTime: moment(this.queryParam.date[1]).format('YYYY-MM-DD'),
          storeId: record.id,
          brandId: this.queryParam.brandId,
          gdClsId: this.queryParam.gdClsId,
        }
        })
          break
          case  '2':
          this.$router.push({
         path: `/report/WholesaleDetail/${record?.id}` ,
         query: {
          startTime: moment(this.queryParam.date[0]).format('YYYY-MM-DD'),
          endTime: moment(this.queryParam.date[1]).format('YYYY-MM-DD'),
          supplyId: record.id,
          brandId: this.queryParam.brandId,
        gdClsId: this.queryParam.gdClsId,
        }
        })
          break
          case '3':
          this.$router.push({
         path: `/report/WholesaleDetail/${record?.id}` ,
         query: {
          startTime: moment(this.queryParam.date[0]).format('YYYY-MM-DD'),
          endTime: moment(this.queryParam.date[1]).format('YYYY-MM-DD'),
          goodsId: record.id,
          brandId: this.queryParam.brandId,
          gdClsId: this.queryParam.gdClsId,
        }
        })
          break
          case '4':
          this.$router.push({
         path: `/report/WholesaleDetail/${record?.id}` ,
         query: {
          startTime: moment(this.queryParam.date[0]).format('YYYY-MM-DD'),
          endTime: moment(this.queryParam.date[1]).format('YYYY-MM-DD'),
          channelId: record.id,
          brandId: this.queryParam.brandId,
          gdClsId: this.queryParam.gdClsId,
        }
        })
          break
          case '5':
          this.$router.push({
         path: `/report/WholesaleDetail/${record?.id}` ,
         query: {
          startTime: moment(this.queryParam.date[0]).format('YYYY-MM-DD'),
          endTime: moment(this.queryParam.date[1]).format('YYYY-MM-DD'),
          areaId: record.id,
          brandId: this.queryParam.brandId,
          gdClsId: this.queryParam.gdClsId,
        }
        })
          break
          case '6':
          this.$router.push({
         path: `/report/WholesaleDetail/${record?.id}` ,
         query: {
          startTime: moment(this.queryParam.date[0]).format('YYYY-MM-DD'),
          endTime: moment(this.queryParam.date[1]).format('YYYY-MM-DD'),
          empId: record.id,
          brandId: this.queryParam.brandId,
          gdClsId: this.queryParam.gdClsId,
        }
        })
          break
        default:
          this.$router.push({
         path: `/report/WholesaleDetail/${record?.id}` ,
         query: {
          startTime: moment(this.queryParam.date[0]).format('YYYY-MM-DD'),
          endTime: moment(this.queryParam.date[1]).format('YYYY-MM-DD'),
          storeId: record.id,
          brandId: this.queryParam.brandId,
          gdClsId: this.queryParam.gdClsId,
        }
        })
          break
      }
    }
  },
}
</script>
